﻿using System;
using System.Reflection;
using NUnit.Framework;
using Smartkernel;

namespace Smartkernel.NUnitTest.
{
    [Category("Core")]
    [TestFixture]
    public class SmartCodeCompilerTest
    {
        [Test]
        public void Test()
        {
            //【01】动态编译程序集（涉及方法：Compile）
            //引用的程序集
            string[] referencedAssemblies = { "system.dll" };
            //待编译的代码文本
            string code = @"
                            using System;
                            namespace Smartkernel.NUnitTest.
                            {
                                public class __SmartCodeCompiler_NUnitTest
                                {
                                    public void Show()
                                    {
                                        Console.Write(5);
                                    }
                                }
                            }
                            ";
            //编译的程序集
            Assembly assembly = null;
            //类的实例
            object instance = null;
            //执行的秒数
            double executeTime1 = 0d;
            executeTime1 = SmartAction.ExecuteTime
            (
                delegate
                {
                    for (int i = 0; i < 3; i++)
                    {
                        assembly = SmartCodeCompiler.Compile(referencedAssemblies, code, false);
                        instance = assembly.CreateInstance("Smartkernel.NUnitTest.__SmartCodeCompiler_NUnitTest");
                        SmartReflection.MethodInvoke("Show", instance);
                        assembly = null;
                        instance = null;
                    }
                }, 5
            ).TotalSeconds;
            double executeTime2 = 0d;
            executeTime2 = SmartAction.ExecuteTime
            (
                delegate
                {
                    for (int i = 0; i < 3; i++)
                    {
                        assembly = SmartCodeCompiler.Compile(referencedAssemblies, code, true);
                        instance = assembly.CreateInstance("Smartkernel.NUnitTest.__SmartCodeCompiler_NUnitTest");
                        SmartReflection.MethodInvoke("Show", instance);
                        assembly = null;
                        instance = null;
                    }
                }, 5
            ).TotalSeconds;
            Assert.AreEqual(executeTime1 > executeTime2, true);
        }
    }
}
